package itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import itheima.reggie.common.R;
import itheima.reggie.entity.ShoppingCart;
import itheima.reggie.service.ShoppingCartService;
import itheima.reggie.utils.BaseContext;
import itheima.reggie.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long user_id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(user_id);
        //判断当前菜品是否在购物车中 d
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, user_id);
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart != null) {
            //菜品在购物车 数量加1
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            try {
                shoppingCartService.updateById(cart);
            } catch (Exception e) {
                e.printStackTrace();
                return R.error("加入失败！！！");
            }
        } else {
            //菜品不在购物车 添加到购物车
            shoppingCart.setCreateTime(DateUtils.DateFormat(new Date()));
            shoppingCartService.save(shoppingCart);
            cart=shoppingCart;
        }
        return R.success("添加成功");
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long user_id = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,user_id);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }

    /**
     * 购物车菜品数量减 1
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        //判断菜品数量是否为1 为1删除 false减1
        if (cartOne.getNumber()==1){
            shoppingCartService.removeById(cartOne);
        }else {
            Integer number = cartOne.getNumber();
            cartOne.setNumber(number-1);
            shoppingCartService.updateById(cartOne);
        }
        return R.success(cartOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }
}
